﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Text;
using SOM.AbstractDAL;
using SOM.Model;
using SOM.SingletonContext;

namespace SOM.ConcreteDAL
{
    public class UsuarioDAL : AbstractUsuarioDAL
    {
        private SOMEntities _contexto;

        public UsuarioDAL()
        {
            _contexto = new SOMEntities();
        }

        /// <summary>
        /// Retorna todas as pessoas cadastradas
        /// </summary>
        /// <returns>Lista de pessoas</returns>
        public override List<Usuario> ReturnAll()
        {

            var consulta = _contexto.UsuariosSet
                .Include("end_endereco");

            return consulta.ToList();
        }

        /// <summary>
        /// Insere um usuário no banco de dados.
        /// </summary>
        /// <param name="usuario"></param>
        public override void Insert(Usuario usuario)
        {
            _contexto.AddToUsuariosSet(usuario);
        }

        public override void Insert(Usuario usuario, SOMEntities contexto)
        {
            _contexto.AddToUsuariosSet(usuario);
            _contexto.SaveChanges();
        }


        /// <summary>
        /// Atualiza um usuario
        /// </summary>
        /// <param name="usuario"></param>
        public override void Update(Usuario usuario)
        {

            _contexto.Attach(usuario);
        }

        public override void Update(Usuario usuario, SOMEntities contexto)
        {
            contexto.Refresh(RefreshMode.ClientWins,usuario);
            contexto.SaveChanges();
        }

        /// <summary>
        /// Remove um usuario
        /// </summary>
        /// <param name="usuario"></param>
        public override void Delete(Usuario usuario)
        {
            _contexto.DeleteObject(usuario);
        }

        /// <summary>
        /// Exclui um usuário.
        /// </summary>
        /// <param name="usuario"></param>
        /// <param name="contexto"></param>
        public override void Delete(Usuario usuario, SOMEntities contexto)
        {
            contexto.DeleteObject(usuario);
            contexto.SaveChanges();
        }

        /// <summary>
        /// Retorna um usuário através do seu nickname.
        /// </summary>
        /// <param name="nickname"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        public override Usuario FindByNickname(string nickname, string include)
        {
            var consulta = from u in _contexto.UsuariosSet.Include(include)
                           where u.usr_ds_nickname.Equals(nickname)
                           select u;

            return consulta.Count() > 0 ? consulta.First() : null;
        }

        /// <summary>
        /// Procura um usuário atrvés do seu CPF.
        /// </summary>
        /// <param name="cpf"></param>
        /// <returns></returns>
        public override Usuario FindByCpf(string cpf)
        {
           using(SOMEntities contexto = new SOMEntities())
           {
               var consulta = from usr in contexto.UsuariosSet
                              where usr.usr_nu_cpf.Equals(cpf)
                              select usr;

               return consulta.Count() == 0 ? null : consulta.First();
           }
        }

        /// <summary>
        /// Carrega um usuário num contexto.
        /// </summary>
        /// <param name="nickname"></param>
        /// <param name="contexto"></param>
        /// <returns></returns>
        public override Usuario Load(string nickname, SOMEntities contexto)
        {
            var consulta = from u in contexto.UsuariosSet.Include("end_endereco")
                           where u.usr_ds_nickname.Equals(nickname)
                           select u;

            return consulta.Count() == 0 ? null : consulta.First();
        }


        /// <summary>
        /// Verifica se um email existe
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public override bool EmailExiste(string email)
        {
            var consulta = from u in _contexto.UsuariosSet
                           where u.usr_ds_email.Equals(email)
                           select u.usr_ds_email;

            return consulta.Count() == 0 ? false : true;
        }

        /// <summary>
        /// Retorna um usuário através do seu nickname.
        /// </summary>
        /// <param name="nickname"></param>
        /// <returns></returns>
        public override Usuario FindByNickname(string nickname)
        {
            var consulta = from u in _contexto.UsuariosSet
                           where u.usr_ds_nickname.Equals(nickname)
                           select u;

            return consulta.Count() > 0 ? consulta.First() : null;

        }

        /// <summary>
        /// Verifica se um CPF existe.
        /// </summary>
        /// <param name="cpf"></param>
        /// <returns></returns>
        public override bool CpfExiste(string cpf)
        {
            var consulta = from u in _contexto.UsuariosSet
                           where u.usr_nu_cpf.Equals(cpf)
                           select u;

            return consulta.Count() > 0 ? true : false;
        }

        public override void SaveChanges()
        {
            _contexto.SaveChanges();
        }
    }
}
